﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;

namespace P.InOut.NetMvc.Controllers
{
    public class UtilitiesController : Controller
    {
        private string _tinyApiUri = "http://tinyurl.com/api-create.php?";
        private string _query = "url=http://";

        public JsonResult getTinyUrl(string url)
        {
            string query = _query + url;
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(_tinyApiUri + query);
            request.KeepAlive = false;

            string response = string.Empty;
            using (HttpWebResponse webResponse = (HttpWebResponse)request.GetResponse())
            {
                using (StreamReader reader = new StreamReader(webResponse.GetResponseStream()))
                {
                    response = reader.ReadToEnd();
                }
            }

            return this.Json(response);
        }
    }
}
